/* @(#)GSAddUnBalanceDetailQueryAction.java
 * 
 *  Project:GS
 *  
 *  Modify  Information:
 *  ===============================================
 *   Author          Date            Description
 *  -------------- ---------------  ---------------
 *  Jenny  Li      2006-6-6          新增
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 *
 */
package com.hitrust.gs.action;

import java.util.ArrayList;
import java.util.HashMap;
import com.hitrust.gs.business.GSBalanceProcessor;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSProviderProcessor;
import com.hitrust.gs.dao.GSOrderDeptInfoDAO;
import com.hitrust.gs.dao.GSOrderDetailDAO;
import com.hitrust.gs.form.GSAddUnBalanceDetaiQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSUnBalanceDetailQryVO;

/**
 * @author JennyLi
 *
 */
public class GSAddUnBalanceDetailQueryAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        GSAddUnBalanceDetaiQueryForm tForm = (GSAddUnBalanceDetaiQueryForm)aForm;
        try{
            //取得页面查询参数
            GSUnBalanceDetailQryVO tOrderDetailModVO = new GSUnBalanceDetailQryVO();
            tOrderDetailModVO.orderDetailId = tForm.orderDetailId;
            tOrderDetailModVO.providerShortName = tForm.providerName;
            tOrderDetailModVO.proverid=tForm.proId;
            tOrderDetailModVO.concentricityId = tForm.concentricityId;
            tOrderDetailModVO.manufInfo = tForm.manufInfo;
            tOrderDetailModVO.manufType1 = tForm.manuftype1;
            tOrderDetailModVO.manufType2 = tForm.manuftype2;
            tOrderDetailModVO.manufType3 = tForm.manuftype3;
            String[] tDateStr = new String[2];
            tDateStr[0] = tForm.orderCreateDateStr;
            tDateStr[1] = tForm.orderCreateDateEnd;
            tDateStr = DateUtil.formateDate(tDateStr);
            tOrderDetailModVO.orderCreateDateStr = tDateStr[0];
            tOrderDetailModVO.orderCreateDateEnd = tDateStr[1];
            tOrderDetailModVO.orderType = tForm.orderType;
            tOrderDetailModVO.deliverGoodsId = tForm.deliverGoodsId;
            tOrderDetailModVO.balanceYM = tForm.balanceYM;
            //存储参数
            GSProviderProcessor ppro=new GSProviderProcessor();
            String proName=ppro.getProviderById(tForm.proId).getGs_provider_shortname();
            aForm.getRequest().setAttribute("proName",proName);
            aForm.getRequest().setAttribute("balanceYM",tForm.balanceYM);
            aForm.getRequest().setAttribute("proid",tForm.proId);
    
            //取得产品分类
            GSManufactureProcessor tManufProc = new GSManufactureProcessor();
            tManufProc.getManufactureOfLevel(aForm);
            //取得分页参数
            String[] tStr = this.getPageParams(tForm,tOrderDetailModVO);
            //执行查询
            HashMap tHm = new HashMap();
            GSBalanceProcessor tBalProc = new GSBalanceProcessor();
            ArrayList tDetailList = tBalProc.getSignedOrderDetailOfDept(tOrderDetailModVO,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
            
            ResultProcess tRsProc = new ResultProcess();
            tRsProc.setResult(tDetailList);
            tForm.getRequest().setAttribute("order",tRsProc);
            //将集中编码对应供应商信息封装传递给前台页面
            tForm.getRequest().setAttribute("provider",tHm);
            
            //存储分页参数
            tForm.getRequest().setAttribute("pageSize",tStr[1]);
            tForm.getRequest().setAttribute("totalRecords",tStr[2]);
        }catch(Exception e){
            tForm.getRequest().setAttribute("window","true");
            throw e;
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }
    private String[] getPageParams(GSBaseForm aForm,GSUnBalanceDetailQryVO aOrderVO) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            GSOrderDetailDAO tDAO = new GSOrderDetailDAO();
            totalRecords = tDAO.queryAddableSignedDetailCount(aOrderVO);
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }
}
